package jrdcom.com.onecodesample.Util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONObject;
import org.w3c.dom.Text;

import jrdcom.com.onecodesample.DB.CoolWearherDB;
import jrdcom.com.onecodesample.Model.CityModel;
import jrdcom.com.onecodesample.Model.CountryModel;
import jrdcom.com.onecodesample.Model.ProvinceModel;
import jrdcom.com.onecodesample.Model.WeatherInfoModel;

/**
 * Created by dhcui on 2017/1/17.
 */
public class HandleUtil {
    public  static synchronized boolean handleProvinceResponse(CoolWearherDB coolWearherDB, String responese){
        if(!TextUtils.isEmpty(responese)){
            String[] allProvinces = responese.split(",");
            if(allProvinces != null && allProvinces.length!=0){
                for(String p :allProvinces){
                    String[] array =p.split("\\|");
                    ProvinceModel provinceModel = new ProvinceModel();
                    provinceModel.setProvinceCode(array[0]);
                    provinceModel.setProvinceName(array[1]);
                    coolWearherDB.saveProvince(provinceModel);
                }
                return true;
            }
        }
        return false;
    }

    public  static  synchronized boolean handleCityResponse(CoolWearherDB coolWearherDB, String response, int ProvinceId){
        if(!TextUtils.isEmpty(response)){
            String[] allCity = response.split(",");
            if(allCity!= null & allCity.length!=0){
                for(String c: allCity){
                    String[] array = c.split("\\|");
                    CityModel cityModel = new CityModel();
                    cityModel.setCityCode(array[0]);
                    cityModel.setCityName(array[1]);
                    cityModel.setProvinceId(ProvinceId);
                    coolWearherDB.saveCity(cityModel);
                }
                return true;
            }
        }
    return false;
    }

    public synchronized static boolean handleCountryResponse(CoolWearherDB coolWearherDB, String response, int CityId){
        if(!TextUtils.isEmpty(response)){
            String[] allCountry  = response.split(",");
            for(String country :allCountry){
                String[] array = country.split("\\|");
                CountryModel countryModel= new CountryModel();
                Log.d("zlwu","zlwu add for handleCountryResponse, array[0] = "+array[0]+"array[1]"+array[1]);
                countryModel.setCountryCode(array[0]);
                countryModel.setCountryName(array[1]);
                countryModel.setCityId(CityId);
                coolWearherDB.saveCountry(countryModel);
            }
            return true;
        }
        return false;
    }

    public  synchronized  static int  handleWeatherCodeRespone(String response){
        int weaCode = 0;
        if(!(TextUtils.isEmpty(response))){
            String[] array = response.split("\\|");
            if(array != null && array.length != 0){
                weaCode = Integer.parseInt(array[1]);//array[1].in;
            }
        }
        return weaCode;
    }

    public  synchronized  static  boolean handleWeatherInfoRespone(Context context,String response, int wea_code){
        Boolean result = false;
        if(!(TextUtils.isEmpty(response))){
            try {
                JSONObject jsonObject= new JSONObject(response);
                JSONObject weatherObject = jsonObject.getJSONObject("weatherinfo");
                WeatherInfoModel weatherInfoModel= new WeatherInfoModel();
                weatherInfoModel.setWeatherCode(wea_code);
                weatherInfoModel.setWeaUpdateTime(weatherObject.getString("ptime"));
                weatherInfoModel.setWeaDes(weatherObject.getString("weather"));
                weatherInfoModel.setWeaTemp1(weatherObject.getString("temp1"));
                weatherInfoModel.setWeaTemp2(weatherObject.getString("temp2"));
                setToLocal(weatherInfoModel, context);
                result = true;
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return result;
    }

    //save data into sharePreference
    public static   void setToLocal(WeatherInfoModel weatherInfoModel, Context context){
        SharedPreferences.Editor editor =  context.getSharedPreferences("weatherInfo", context.MODE_PRIVATE).edit();
        editor.putInt("weaCode",weatherInfoModel.getWeatherCode());
        editor.putString("weaUpdateTime", weatherInfoModel.getWeaUpdateTime());
        editor.putString("weaDes", weatherInfoModel.getWeaDes());
        editor.putString("weaTmp1", weatherInfoModel.getWeaTemp1());
        editor.putString("weaTmp2", weatherInfoModel.getWeaTemp2());
        editor.commit();
    }
    //get data local
    public static WeatherInfoModel getLocalInfo(Context context){
        WeatherInfoModel weatherInfoModel = new WeatherInfoModel();
        SharedPreferences pre= context.getSharedPreferences("weatherInfo", context.MODE_PRIVATE);
        weatherInfoModel.setWeatherCode(pre.getInt("weaCode", 0));
        weatherInfoModel.setWeaUpdateTime(pre.getString("weaUpdateTime", null));
        weatherInfoModel.setWeaDes(pre.getString("weaDes", null));
        weatherInfoModel.setWeaTemp1(pre.getString("weaTmp1", null));
        weatherInfoModel.setWeaTemp2(pre.getString("weaTmp2",null));
        return weatherInfoModel;
    }
}

